from datetime import datetime
from django.db.models import F
import copy
import re
from rest_framework.permissions import IsAuthenticated
from apps.vadmin.op_drf.viewsets import CustomModelViewSet
from apps.vadmin.op_drf.response import SuccessResponse, ErrorResponse
from apps.vadmin.utils.exceptions import APIException
from apps.vadmin.assets.enums import AssetsEnum,AssignedTypeEnum
from apps.vadmin.permission.models.users import UserProfile

from apps.vadmin.assets.models.locations import Locations
from apps.vadmin.assets.models.assets import Assets
from apps.vadmin.assets.models.accessories import Accessories
from apps.vadmin.assets.models.components import Components
from apps.vadmin.assets.models.licenses import Licenses
from apps.vadmin.assets.models.consumables import Consumables
from apps.vadmin.assets.models.components_assets import Components_Assets 
from apps.vadmin.assets.models.accessories_users import Accessories_Users
from apps.vadmin.assets.models.consumables_users import Consumables_Users 
from apps.vadmin.assets.models.license_seats import License_Seats
from apps.vadmin.assets.models.kits_models import KitsModels
from apps.vadmin.assets.models.report import AssetNowCheckoutReport

from apps.vadmin.assets.models.check_records import CheckRecords 
from apps.vadmin.assets.serializers import CheckRecordsSerializer
from apps.vadmin.assets.serializers import ComponentsAssetsSerializer
from apps.vadmin.assets.serializers import AccessoriesUsersSerializer
from apps.vadmin.assets.serializers import ConsumablesUsersSerializer
from apps.vadmin.assets.serializers import LicenseSeatsSerializer
from apps.vadmin.assets.serializers import AssetNowCheckoutReportSerializer

from apps.vadmin.assets.filters import CheckRecordsFilter
from apps.vadmin.assets.filters import ComponentsAssetsFilter
from apps.vadmin.assets.filters import ConsumablesUsersFilter
from apps.vadmin.assets.filters import AccessoriesUsersFilter
from apps.vadmin.assets.filters import LicenseSeatsFilter
from apps.vadmin.assets.filters import AssetNowCheckoutReportFilter


def is_valid_date(date_str):
    pattern = r"^\d{4}-\d{2}-\d{2}$"
    return bool(re.match(pattern, date_str))

class CheckRecordsModelViewSet(CustomModelViewSet):
    permission_classes = [IsAuthenticated]
    queryset = CheckRecords.objects.all()
    serializer_class = CheckRecordsSerializer
    filter_class = CheckRecordsFilter

    def loadComData(self,request,md):
        md["creator"] = request.user.id
        md["modifier"] = request.user.username
        md["dept_belong_id"] = getattr(self.request.user, 'dept_id', None)
    
    def asset_audit(self, request, *args, **kwargs):
        # 资产审核,支持根据资产id或资产标签审核
        asset_id = request.data.get("asset_id",None) 
        asset_tag = request.data.get("asset_tag",None) 
        location_id = request.data.get("location_id",None) 
        next_audit_date = request.data.get("next_audit_date",None) 
        image = request.data.get("image",None) 
        request.data['qty'] = 1
        request.data['asset_type'] = "asset" 
        request.data["check_datetime"] = datetime.now()

        dbasset = None
        if asset_id is not None:
            dbasset = Assets.objects.get(id=asset_id)
        elif asset_tag is not None:
            dbasset = Assets.objects.get(asset_tag=asset_tag)
        if dbasset is None:
            raise APIException(message='不存在该资产')
        if location_id is not None:
            location = Locations.objects.get(id=location_id)
            dbasset.location = location
            request.data['assigned_to_id'] = location.id
            request.data['assigned_type'] = "location"
            request.data['assigned_to_name'] = location.name
        if image is not None:
            dbasset.image = image

        dbasset.last_audit_date = datetime.now() 
        dbasset.next_audit_date = next_audit_date
        dbasset.save() 
        request.data['option'] = "audit" 
        request.data['asset_id'] = dbasset.id
        request.data['asset_name'] = dbasset.name
        request.data['expected_checkin_date'] = next_audit_date 
        super().create(request, *args, **kwargs)
        return SuccessResponse() 

    def kits_checkout(self, request, *args, **kwargs):
        assigned_to_id = request.data.get('assigned_to_id',None)
        kit_id = request.data.get('kit_id',None)
        check_datetime = request.data.get("check_datetime",None)
        if check_datetime is None:
            request.data["check_datetime"] = datetime.now()
        elif is_valid_date(check_datetime):
            check_datetime = check_datetime + " 00:00:00"
            request.data["check_datetime"] = check_datetime
        else:
            request.data["check_datetime"] = check_datetime
        expected_checkin_date = request.data.get('expected_checkin_date',None)
        assigned_type = request.data.get("assigned_type",None) # 借出类型 asset/user/location
        a = None
        if assigned_type == "asset":
            a = Assets.objects.get(id=assigned_to_id)
        elif assigned_type == "user":
            a = UserProfile.objects.get(id=assigned_to_id)
        elif assigned_type == "location":
            a = Locations.objects.get(id=assigned_to_id)
        else: 
            raise APIException(message="只能借给asset/user/location")
        error_list = []
        can_assign_asset_list = []
        model_list = KitsModels.objects.filter(kit_id=kit_id)
        if len(model_list) == 0:
            raise APIException(message=f'未发现kit_id={kit_id}')
        for record in model_list:
           asset_list = Assets.objects.filter(model_id=record.model.id,assigned_to_id=None,status='deployable',deleted_datetime__isnull=True) 
           num = len(asset_list)
           if  num < record.quantity :
               error_list.append(f'{record.model.name}的可分配资产数量只剩下{num}')
           else :
               can_assign_asset_list.append(asset_list)
        if len(error_list) == 0 :
            for queryset in can_assign_asset_list:
                for asset in queryset:
                    asset.assigned_to_id = assigned_to_id
                    asset.assigned_to_name = a.name
                    asset.assigned_type = assigned_type
                    asset.expected_checkin = expected_checkin_date
                    asset.last_checkout = datetime.now()
                    asset.checkout_counter = F('checkout_counter') + 1
                    request.data['asset_id'] = asset.id
                    request.data['asset_name'] = asset.name
                    request.data['assigned_to_name'] = a.name
                    request.data['qty'] = 1
                    request.data['option'] = "checkout" 
                    asset.save()
                    super().create(request, *args, **kwargs)
            
        else :
            raise APIException(message='\n'.join(error_list))

        return SuccessResponse() 

    def asset_batch_checkout(self, request, *args, **kwargs):
        # 资产批量借出
        assigned_type = request.data.get("assigned_type",None) # 借出类型 asset/user/location
        assigned_to_id = request.data.get('assigned_to_id',None)
        expected_checkin = request.data.get('expected_checkin_date',None)
        request.data['qty'] = 1
        request.data['option'] = "checkout" 

        asset_ids = request.data.get("checkout_ids")
        asset_list = Assets.objects.filter(id__in=asset_ids)

        a = None
        if assigned_type == "asset":
            a = Assets.objects.get(id=assigned_to_id)
        elif assigned_type == "user":
            a = UserProfile.objects.get(id=assigned_to_id)
        elif assigned_type == "location":
            a = Locations.objects.get(id=assigned_to_id)
        else: 
            raise APIException(message="只能借给asset/user/location")

        request.data['assigned_to_name'] = a.name
        for asset in asset_list:
            if asset.deleted_datetime is not None:
                raise APIException(message=f'{asset_id}已删除,不可借出！')
            if asset.status != str(AssetsEnum.DEPLOYABLE):
                raise APIException(message=f'{asset_id}不可借出！')
            if asset.assigned_to_id is not None:
                raise APIException(message=f'{asset.name},id={asset.id}已借出，不能再借出！')
            asset.assigned_to_id = assigned_to_id
            asset.assigned_to_name = a.name
            asset.assigned_type = assigned_type
            asset.expected_checkin = expected_checkin
            asset.last_checkout = datetime.now()
            asset.checkout_counter = F('checkout_counter') + 1
            request.data['asset_id'] = asset.id
            request.data['asset_name'] = asset.name
            asset.save()
            super().create(request, *args, **kwargs)
        return SuccessResponse() 

    def asset_checkout(self, request, *args, **kwargs):
        # 资产借出逻辑
        asset_id= request.data.get("checkout_id",None) # 
        assigned_type = request.data.get("assigned_type",None) # 借出类型 asset/user/location
        expected_checkin = request.data.get('expected_checkin_date',None)
        assigned_to_id = request.data.get('assigned_to_id',None)
        request.data['qty'] = 1
        request.data['option'] = "checkout" 

        asset = Assets.objects.get(id=asset_id)
        request.data['asset_id'] = asset.id
        request.data['asset_name'] = asset.name

        if asset.deleted_datetime is not None:
             raise APIException(message=f'{asset_id}已删除,不可借出！')
        if asset.status != str(AssetsEnum.DEPLOYABLE):
            raise APIException(message=f'{asset_id}不可借出！')
        if asset.assigned_to_id is not None:
            raise APIException(message=f'{asset.name}已借出，不能再借出！')
        asset.assigned_to_id = assigned_to_id
        if assigned_type == "asset":
            a = Assets.objects.get(id=assigned_to_id)
            asset.assigned_to_name = a.name
            request.data['assigned_to_name'] = a.name
        elif assigned_type == "user":
            a = UserProfile.objects.get(id=assigned_to_id)
            asset.assigned_to_name = a.name
            request.data['assigned_to_name'] = a.name
        elif assigned_type == "location":
            a = Locations.objects.get(id=assigned_to_id)
            asset.assigned_to_name = a.name
            request.data['assigned_to_name'] = a.name
        else: 
            raise APIException(message="只能借给asset/user/location")
        asset.assigned_type = assigned_type
        asset.expected_checkin = expected_checkin
        asset.last_checkout = datetime.now()
        asset.checkout_counter = F('checkout_counter') + 1
        asset.save()
        super().create(request, *args, **kwargs)
        return SuccessResponse() 


    def asset_checkin(self, request, *args, **kwargs):
        # 资产归还逻辑
        asset_id = request.data.get("record_id",None) # 
        asset_tag = request.data.get("asset_tag",None) # 
        request.data['option'] = "checkin" 
        request.data['qty'] = -1 
        try:
            asset = None
            if asset_id is not None:
                asset = Assets.objects.get(id=asset_id)
            elif asset_tag is not None:
                asset = Assets.objects.get(asset_tag=asset_tag)
            if asset.assigned_to_id is None:
                raise APIException(message=f'{asset_id}未借出，无需归还！')

            

            location_id = request.data.get("location_id",None) # 
            if location_id is None:
                location_id = request.data.get("location",None) # 
            if location_id is not None:
                location = Locations.objects.get(id=location_id)
                request.data['assigned_to_id'] = location_id
                request.data['assigned_type'] = 'location'
                request.data['assigned_to_name'] = location.name
                asset.location = location
            else:
                request.data['assigned_to_id'] = asset.assigned_to_id
                request.data['assigned_type'] = asset.assigned_type
                request.data['assigned_to_name'] = asset.assigned_to_name

            request.data['asset_id'] = asset.id
            request.data['asset_name'] = asset.name

            asset.checkin_counter = F('checkin_counter') + 1
            asset.assigned_to_id = None
            asset.assigned_to_name  = None
            asset.expected_checkin = None
            asset.assigned_type = None
            asset.save()
            super().create(request, *args, **kwargs)
            return SuccessResponse() 

        except Assets.DoesNotExist:
            raise APIException(message=f'{asset_id}不存在！')

    def component_checkout(self,request, *args, **kwargs) :
        # 组件借出逻辑
        component_id = request.data.get("checkout_id",None)
        assigned_to_id = request.data.get('assigned_to_id',None)
        qty = int(request.data.get("qty",1))
        request.data['option'] = "checkout" 
        request.data['assigned_to'] = "asset" 
        request.data['qty'] = qty 

        try:
            component = Components.objects.get(id=component_id);
            request.data['asset_id'] = component.id
            request.data['asset_name'] = component.name
            asset = Assets.objects.get(id=assigned_to_id)
            request.data['assigned_to_name'] = asset.name
            checkout_qty = component.checkout_qty if component.checkout_qty is not None else 0
            last_qty = component.qty - checkout_qty
            if last_qty < qty:
                raise APIException(message=f'{component.name}数量只剩下{last_qty}！')

            component.checkout_qty = F("checkout_qty") + qty

            componentAsset = copy.deepcopy(request.data)
            componentAsset["component"] = component.id
            componentAsset["asset"] = asset.id
            self.loadComData(request,componentAsset)
            serializer = ComponentsAssetsSerializer(data=componentAsset)
            if serializer.is_valid():
                serializer.save()
                component.save()
                super().create(request, *args, **kwargs)
            else:
                raise APIException(message=str(serializer.errors))
            return SuccessResponse() 

        except Components.DoesNotExist:
            raise APIException(message=f'不存在{component_id}！')
        except Assets.DoesNotExist:
            raise APIException(message=f'{assigned_to_id}不存在！')

    def component_checkin(self,request, *args, **kwargs) :
        # 组件归还逻辑
        record_id = request.data.get("record_id",None)
        qty = request.data.get("qty",1)
        qty = abs(int(qty))*-1
        request.data['option'] = "checkin" 
        request.data['qty'] = qty
        request.data['assigned_type'] = "asset"
        try: 
           componentAsset = Components_Assets.objects.get(id=record_id)
           request.data['asset_id'] = componentAsset.component.id
           request.data['asset_name'] = componentAsset.component.name
           request.data['assigned_to_id'] = componentAsset.asset.id
           request.data['assigned_to_name'] = componentAsset.asset.name
           componentAsset.component.checkout_qty = F("checkout_qty") + qty
           if componentAsset.qty + qty <=0:
               componentAsset.delete()
               componentAsset.component.save()
               super().create(request, *args, **kwargs)
           else:
               componentAsset.qty = componentAsset.qty + qty
               componentAsset.component.save()
               componentAsset.save()
               super().create(request, *args, **kwargs)
           return SuccessResponse() 

        except Components_Assets.DoesNotExist:
            raise APIException(message=f'不存在{record_id}的借出记录')


    def accessory_checkout(self,request, *args, **kwargs) :
        # 配件借出逻辑
        accessory_id = request.data.get("checkout_id",None)
        assigned_to_id = request.data.get('assigned_to_id',None)
        qty = int(request.data.get("qty",1))
        request.data['option'] = "checkout" 
        request.data['qty'] = qty
        request.data['assigned_type'] = "user"

        try:
            accessory = Accessories.objects.get(id=accessory_id);
            user = UserProfile.objects.get(id=assigned_to_id)
            last_qty = accessory.qty - accessory.checkout_qty
            request.data['asset_id'] = accessory.id
            request.data['asset_name'] = accessory.name
            request.data['assigned_to_name'] = user.name
            if last_qty < qty:
                raise APIException(message=f'{accessory.name}数量只剩下{last_qty}！')


            accessoriesUser = copy.deepcopy(request.data)
            accessoriesUser["accessory"] = accessory.id
            accessoriesUser["assigned_to"] = user.id
            self.loadComData(request,accessoriesUser)
            serializer = AccessoriesUsersSerializer(data=accessoriesUser)
            if serializer.is_valid():
                serializer.save()
                accessory.checkout_qty = F("checkout_qty") + qty 
                accessory.save()
                super().create(request, *args, **kwargs)
            else:
                raise APIException(message=str(serializer.errors))
            return SuccessResponse() 

        except Accessories.DoesNotExist:
            raise APIException(message=f'不存在{accessory_id}！')
        except UserProfile.DoesNotExist:
            raise APIException(message=f'{assigned_to_id}不存在！')
    
    def accessory_checkin(self,request, *args, **kwargs) :
        # 配件归还逻辑
        record_id = request.data.get("record_id",None)
        request.data['qty'] = -1 
        request.data['option'] = "checkin" 

        try:
            accessoryUser = Accessories_Users.objects.get(id=record_id)

            accessoryUser.accessory.checkout_qty = F("checkout_qty") - 1 

            request.data['asset_id'] = accessoryUser.accessory.id
            request.data['asset_name'] = accessoryUser.accessory.name
            request.data['assigned_to_id'] = accessoryUser.assigned_to.id
            request.data['assigned_type'] = "user"
            request.data['assigned_to_name'] = accessoryUser.assigned_to.name

            accessoryUser.delete()
            accessoryUser.accessory.save()
            super().create(request, *args, **kwargs)
            return SuccessResponse() 

        except Accessories_Users.DoesNotExist:
            raise APIException(message=f'不存在{record_id}的借出记录')

    def consumable_checkout(self,request, *args, **kwargs) :
        # 消耗品借出逻辑
        consumable_id = request.data.get("checkout_id",None)
        assigned_to_id = request.data.get('assigned_to_id',None)
        qty = int(request.data.get("qty",1))
        request.data['option'] = "checkout" 
        request.data['qty'] = qty 
        request.data['assigned_type'] = "user"

        try:
            consumable = Consumables.objects.get(id=consumable_id);
            user = UserProfile.objects.get(id=assigned_to_id)
            last_qty = consumable.qty - consumable.checkout_qty
            if last_qty < qty:
                raise APIException(message=f'{consumable.name}数量只剩下{last_qty}！')

            request.data['asset_id'] = consumable.id
            request.data['asset_name'] = consumable.name
            request.data['assigned_to_name'] = user.name

            consumablesUser = copy.deepcopy(request.data)
            consumablesUser["consumable"] = consumable.id
            consumablesUser["assigned_to"] = user.id
            self.loadComData(request,consumablesUser)
            serializer = ConsumablesUsersSerializer(data=consumablesUser)
            if serializer.is_valid():
                serializer.save()
                consumable.checkout_qty = F("checkout_qty") + qty 
                consumable.save()
                super().create(request, *args, **kwargs)
            else:
                raise APIException(message=str(serializer.errors))
            return SuccessResponse() 

        except Consumables.DoesNotExist:
            raise APIException(message=f'不存在{consumeable_id}！')
        except UserProfile.DoesNotExist:
            raise APIException(message=f'{assigned_to_id}不存在！')

    def license_checkout(self,request, *args, **kwargs) :
        # 软件授权借出逻辑
        license_id = request.data.get("checkout_id",None)
        assigned_to_id = request.data.get('assigned_to_id',None)
        assigned_type = request.data.get('assigned_type',None)
        qty = int(request.data.get("qty",1))
        request.data['option'] = "checkout" 
        request.data['qty'] = qty 

        try:
            license = Licenses.objects.get(id=license_id)
            request.data['asset_id'] = license.id
            request.data['asset_name'] = license.name
            last_qty = license.seats  - license.checkout_seats
            if last_qty < qty:
                raise APIException(message=f'{license.name}数量只剩下{last_qty}！')
            licenseSeat = copy.deepcopy(request.data)
            licenseSeat['license'] = license.id
            if "asset" == assigned_type:
                licenseSeat["asset"] = assigned_to_id
            elif "user" == assigned_type:
                licenseSeat["assigned_to"] = assigned_to_id
            else:
                raise APIException(message='assigned_type必须是asset或user')
            self.loadComData(request,licenseSeat)
            serializer = LicenseSeatsSerializer(data=licenseSeat)
            if serializer.is_valid():
                if "asset" == assigned_type:
                    request.data['assigned_to_name'] = serializer.validated_data['asset'].name
                elif "user" == assigned_type:
                    request.data['assigned_to_name'] = serializer.validated_data['assigned_to'].name
                serializer.save()
                license.checkout_seats = F("checkout_seats") + qty
                license.save()
                super().create(request, *args, **kwargs)
            else:
                raise APIException(message=str(serializer.errors))
            return SuccessResponse() 

        except Licenses.DoesNotExist:
            raise APIException(message=f'不存在{license_id}！')


    def license_checkin(self,request, *args, **kwargs) :
        # 软件许可归还逻辑
        record_id = request.data.get("record_id",None)
        request.data['qty'] = -1 
        request.data['option'] = "checkin" 

        try:
            licenseSeat  = License_Seats.objects.get(id=record_id)
            licenseSeat.license.checkout_seats = F("checkout_seats") - 1 

            request.data['asset_id'] = licenseSeat.license.id
            request.data['asset_name'] = licenseSeat.license.name
            if licenseSeat.assigned_to is not None:
                request.data['assigned_type'] = "user"
                request.data['assigned_to_id'] = licenseSeat.assigned_to.id
                request.data['assigned_to_name'] = licenseSeat.assigned_to.name
            elif licenseSeat.asset is not None:
                request.data['assigned_type'] = "asset"
                request.data['assigned_to_id'] = licenseSeat.asset.id
                request.data['assigned_to_name'] = licenseSeat.asset.name

            licenseSeat.delete()
            licenseSeat.license.save()
            super().create(request, *args, **kwargs)
            return SuccessResponse() 

        except License_Seats.DoesNotExist:
            raise APIException(message=f'不存在{record_id}的借出记录')

    def checkout(self, request, *args, **kwargs):
        # 借出逻辑 
        asset_type = request.data.get("asset_type",None) # 
        check_datetime = request.data.get("check_datetime",None)
        if check_datetime is None:
            request.data["check_datetime"] = datetime.now()
        elif is_valid_date(check_datetime):
            check_datetime = check_datetime + " 00:00:00"
            request.data["check_datetime"] = check_datetime
        else:
            request.data["check_datetime"] = check_datetime
        if "asset" == asset_type:
            return self.asset_checkout(request, *args, **kwargs) 
        elif "component" == asset_type:
            return self.component_checkout(request, *args, **kwargs) 
        elif "accessory" == asset_type:
            return self.accessory_checkout(request, *args, **kwargs) 
        elif "license" == asset_type:
            return self.license_checkout(request, *args, **kwargs) 
        elif "consumable" == asset_type:
            return self.consumable_checkout(request, *args, **kwargs) 
        else:
            raise APIException(message="asset_type error")

    def checkin(self, request, *args, **kwargs):
        # 归还逻辑
        check_datetime = request.data.get("check_datetime",None)
        if check_datetime is None:
            request.data["check_datetime"] = datetime.now()
        elif is_valid_date(check_datetime):
            check_datetime = check_datetime + " 00:00:00"
            request.data["check_datetime"] = check_datetime
        else:
            request.data["check_datetime"] = check_datetime

        asset_type = request.data.get("asset_type",None) # 
        if "asset"==asset_type:
            return self.asset_checkin(request, *args, **kwargs) 
        elif "component" == asset_type:
            return self.component_checkin(request, *args, **kwargs) 
        elif "accessory" == asset_type:
            return self.accessory_checkin(request, *args, **kwargs) 
        elif "license" == asset_type:
            return self.license_checkin(request, *args, **kwargs) 
        else:
            raise APIException(message="asset_type error")

    def update(self, request, *args, **kwargs):
        # 不做任何处理，返回一个空响应
        return Response(status=status.HTTP_204_NO_CONTENT)

    def destroy(self, request, *args, **kwargs):
        # 不做任何处理，返回一个空响应
        return Response(status=status.HTTP_204_NO_CONTENT)

class ComponentsAssetsModelViewSet(CustomModelViewSet):
    '''
    资产组件借出记录模型的CRUD视图
    '''
    permission_classes = [IsAuthenticated]
    queryset = Components_Assets.objects.all()
    serializer_class = ComponentsAssetsSerializer
    filter_class= ComponentsAssetsFilter
    # extra_filter_backends = [DataLevelPermissionsFilter]

    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)


class LicenceRecordsModelViewSet(CustomModelViewSet):
    '''
    软件许可借出记录模型的CRUD视图
    '''
    permission_classes = [IsAuthenticated]
    queryset = License_Seats.objects.all()
    serializer_class = LicenseSeatsSerializer 
    filter_class= LicenseSeatsFilter
    # extra_filter_backends = [DataLevelPermissionsFilter]

    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)
         
class AccessoryRecordsModelViewSet(CustomModelViewSet):
    '''
    附属品借出记录模型的CRUD视图
    '''
    permission_classes = [IsAuthenticated]
    queryset = Accessories_Users.objects.all()
    serializer_class = AccessoriesUsersSerializer
    filter_class= AccessoriesUsersFilter
    # extra_filter_backends = [DataLevelPermissionsFilter]

class ConsumableRecordsModelViewSet(CustomModelViewSet):
    '''
    软件许可借出记录模型的CRUD视图
    '''
    permission_classes = [IsAuthenticated]
    queryset = Consumables_Users.objects.all()
    serializer_class = ConsumablesUsersSerializer
    filter_class= ConsumablesUsersFilter
    # extra_filter_backends = [DataLevelPermissionsFilter]
    

class AssetNowCheckoutModelViewSet(CustomModelViewSet):
    '''
    当前各种资产借出情况
    '''
    permission_classes = [IsAuthenticated]
    queryset = AssetNowCheckoutReport.objects.all()
    serializer_class = AssetNowCheckoutReportSerializer
    filter_class= AssetNowCheckoutReportFilter
    # extra_filter_backends = [DataLevelPermissionsFilter]
    

